﻿var SelectedColorFocused = "#adc3e7";
var SelectedColorUnfocused = "#eeeeee";
var _mode = 0;


var LookupBrowse = 0x1;
var LookupShowColumns = 0x2;
var LookupMultiSelect = 0x4;

function checkMode(mode, option)
{
return ((mode & option) == option);
}

function search()
{
document.body.style.cursor = "wait";

var url = "lookupdata.aspx";

url += window.location.search;
url += "&Mode=" + _mode;




url += "&ObjectType=" + selObjects.value;

if (!checkMode(_mode, LookupBrowse))
{
url += "&searchvalue=" + encodeURIComponent(findValue.value);
btnGo.disabled = true;
findValue.focus();
}

frmResults.location.href = url;
}

function selectChange(o)
{


if (checkMode(_mode, LookupBrowse))
{
search();
}

switch (parseInt(o.DataValue, 10))
{
case Account:		btnNew.disabled = !_canCreateAccount;		break;
case Contact:		btnNew.disabled = !_canCreateContact;		break;
case Lead:			btnNew.disabled = !_canCreateLead;			break;
case Opportunity:	btnNew.disabled = !_canCreateOpportunity;	break;

default:

btnNew.disabled = true;

break;
}
}


function buildReturnValue(rows)
{
    var lookupItems = new LookupItems();

    var len = rows.length;

    for (var i = 0; i < len; i++)
    {
        var tr = rows[i];
        var columns = tr.parentElement.parentElement.getElementsByTagName("col");
        
        lookupItems.add(tr, columns)
    }
    return lookupItems;
}



function initSelectedItems(table)
{
if (table.selectedItems == undefined)
{
table.selectedItems = new Array();
}
}

function getActiveItem(elem)
{
while (elem.tagName != "TR")
{
elem = elem.parentElement;
}

return elem;
}

function unselectItems(table)
{
var multiSelect = checkMode(_mode, LookupMultiSelect);

if (table.selectedItems == undefined) return;
if (multiSelect && event.ctrlKey && event.keyCode == 0) return;

while (table.selectedItems.length > 0)
{
unselectItem(table, table.selectedItems[0]);
}
}

function unselectItem(table, item)
{
if (table.selectedItems == undefined) return;

var items = table.selectedItems;
for (var i = 0; i < items.length; i++)
{
if (items[i] == item)
{
items[i].selected = 0;
items[i].runtimeStyle.backgroundColor = "";
table.selectedItems.splice(i, 1);
break;
}
}
}

function selectItem(table, item, focused, fShiftSelect)
{
if (item.unselectable != undefined) return;



initSelectedItems(table);

item.selected = 1;

if (focused)
item.runtimeStyle.backgroundColor = SelectedColorFocused;
else
item.runtimeStyle.backgroundColor = SelectedColorUnfocused;


if (IsNull(table.lastSelected) || IsNull(fShiftSelect) || !fShiftSelect)
{
table.lastSelected = item;
}



table.selectedItems.push (item);



if ( table.id == "tblResults" && "undefined" != typeof(parent.btnProperties))
{
parent.btnProperties.disabled = (item.oid == undefined);
}
}

function shiftSelectItems(table, o)
{
if (table.lastSelected != undefined)
{
var lastSelected = table.lastSelected;


unselectItems(table);


selectItem(table, lastSelected, true, false);

if (lastSelected.rowIndex >= o.rowIndex)
{
var rows = table.rows;
for (var i = o.rowIndex; i < lastSelected.rowIndex; i++)
{
selectItem(table, rows[i], true, true);
}
}
else
{
var rows = table.rows;

for (var i = lastSelected.rowIndex + 1; i <= o.rowIndex; i++)
{
selectItem(table, rows[i], true, true);
}
}
}
}

function clickItem(table)
{
if (event.srcElement.tagName == "TBODY") return;

var item = getActiveItem(event.srcElement);
var multiSelect = checkMode(_mode, LookupMultiSelect);

if (multiSelect && event.shiftKey)
{
shiftSelectItems(table, item);
}
else if (multiSelect && event.ctrlKey && item.selected)
{
unselectItem(table, item, true);
}
else
{
unselectItems(table);

selectItem(table, item, true);
}
return item;
}

function findValueKeyDown()
{
if (event.keyCode == 13)
{
search();
}
}



function listKeyDown(table)
{
if (event.keyCode == 32)
{
table.ondblclick();
}
else if (event.keyCode == 38)
{
var item = table.lastSelected;

if (item && item.rowIndex > 0)
{
item = item.previousSibling;

if (item.unselectable != undefined)
{
item = item.previousSibling;

if (item == null)
return;
}

unselectItems(table);

selectItem(table, item, true);


if((item.rowIndex * item.clientHeight) < table.parentElement.scrollTop)
{
item.scrollIntoView(true);
}
}



window.event.returnValue = false;
}
else if (event.keyCode == 40)
{
var item = table.lastSelected;

if (item && item.rowIndex < table.rows.length- 1)
{
item = item.nextSibling;

if (item.unselectable != undefined)
{
item = item.nextSibling;

if (item == null)
return;
}

unselectItems(table);

selectItem(table, item, true);


if(((item.rowIndex + 1) * item.clientHeight) > (table.parentElement.scrollTop + table.parentElement.clientHeight))
{
item.scrollIntoView(false);
}
}



window.event.returnValue = false;
}
else if (!(event.shiftKey || event.ctrlKey) && ((event.keyCode > 47 && event.keyCode < 58) || (event.keyCode > 64 && event.keyCode < 91)))
{
var code;
var len = table.rows.length;

for (i = (checkMode(_mode, LookupMultiSelect)) ? 0 : 2; i < len; i++)
{
code = table.rows[i].cells[0].innerText.charCodeAt(0);

if ((code == event.keyCode) || ((event.keyCode > 64 && event.keyCode < 91) && (code == event.keyCode + 32)))
{
item = table.rows[i];

unselectItems(table);

selectItem(table, item, true);

item.scrollIntoView(true);

break;
}
}
}
}



function focusSelectedItems(table, focused)
{
if (table.selectedItems == undefined) return;

var items = table.selectedItems;



if (items.length == 0 && table.rows.length > 0)
{
selectItem(table, table.rows[0], true);
}

for (var i = 0; i < items.length; i++)
{
if (focused)
{
items[i].runtimeStyle.backgroundColor = SelectedColorFocused;
}
else
{
items[i].runtimeStyle.backgroundColor = SelectedColorUnfocused;
}
}
}

function showProperties()
{
var items = frmResults.tblResults.selectedItems;

if (items.length == 0)
{
alert(LOCID_SELECT_AN_OBJECT);
}
else if (items.length > 1)
{
alert(LOCID_SELECT_ONE_OBJECT);
}
else
{
switch (Number(items[0].otype))
{
case Service:
openStdWin("/sm/services/readonly.aspx?objTypeCode=" + items[0].otype + "&id=" + items[0].oid, "readonly" + buildWinName(items[0].oid), 560, 525);
break;
default:
openStdWin("/_forms/readonly/readonly.aspx?objTypeCode=" + items[0].otype + "&id=" + items[0].oid, "readonly" + buildWinName(items[0].oid), 560, 525);
break;
}
}
}

function createNew()
{
var returnObject;




returnObject = openStdDlg('/_forms/QuickCreateReturn/quickcreate.aspx?crmFormSubmitObjectType=' + selObjects.value, selObjects.value, 400, 400);

if (returnObject)
{
findValue.value = returnObject.name;
search();
}
}



function LookupItem()
{


this.id = "";
this.name = "";
this.html = "";
this.type = "";
this.values = null;
this.keyValues = null;
}

function LookupItemData(name, value)
{


this.name = name;
this.value = value;
}

function LookupItems()
{
    this.add = _add;
    this.items = new Array();

    function _add(tr, columns)
    {
        var li = new LookupItem();
        var td = tr.cells[0];

        li.id		= tr.oid;
        li.name		= td.innerText;
        li.html		= td.innerHTML;
        li.type		= tr.otype;

//        if (tr.originalItem)
//        {
//            if (!IsNull(tr.originalItem.getAttribute("exchangeentryid")))
//            {
//                li.exchangeEntryId = tr.originalItem.getAttribute("exchangeentryid");
//            }

//            if (!IsNull(tr.originalItem.getAttribute("activitypartyid")))
//            {
//                li.activityPartyId = tr.originalItem.getAttribute("activitypartyid");
//            }
//        }

        li.values	= new Array();
        li.keyValues	= new Array();

        var len = columns.length;
        if (len > 1)
        {
            for (var i = 1; i < len; ++i)
            {
                li.keyValues[new String(columns[i].name)] = new LookupItemData(columns[i].name, tr.cells[i].innerText)
                li.values.push(new LookupItemData(columns[i].name, tr.cells[i].innerText));
            }
        }

        this.items.push (li);
    }
    
}


function RenderToolTip()
{
var o = event.srcElement;

if (o && o.tagName == "NOBR" && o.title == "" && o.innerText != "")
{
o.title = o.innerText;
}
}

function lookupPageReady()
{
if(document.readyState == "complete")
{
setNavigationState();
}
else
{
window.setTimeout("lookupPageReady", 500);
}
}
